package com.kingsoft.dc.khaos.extender.meta.utils;

import com.kingsoft.dc.khaos.extender.exception.MetaException;
import com.kingsoft.dc.khaos.extender.meta.api.CmRequest;
import com.kingsoft.dc.khaos.extender.meta.api.DmRequest;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by haorenhui on 2020/11/03
 * 运维中心指标上报
 */
public class CmReportUtils {
    private static String className = "com.kingsoft.dc.khaos.extender.meta.api.CmApiUtils";
    private static String REPORT_SYNCPROCESSDATA = "reportSyncProcessData";
    public static <T> T convertClass(Object obj, Class<T> tClass) {
        return (T) obj;
    }

    public static <T> T callMethod(String methodName, CmRequest request, Class<T> tClass) {
        Class clazz = null;
        try {
            clazz = Class.forName(className);
            Object ds = clazz.getMethod(methodName, CmRequest.class).invoke(clazz.newInstance(), new Object[]{request});
            return convertClass(ds, tClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            throw new MetaException(e.getMessage());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T callMethod(String methodName, CmRequest request) {
        Class clazz = null;
        try {
            clazz = Class.forName(className);
            Object ds = clazz.getMethod(methodName, CmRequest.class).invoke(clazz.newInstance(), new Object[]{request});
            return (T)ds;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T reportSyncProcessData(CmRequest request, Class<T> tClass) {
        return callMethod(REPORT_SYNCPROCESSDATA, request, tClass);
    }

}